# coding:utf-8
__author__ = 'Administrator'

'''
1.open keep
2.click 首页
3.click 热门
4.循环点击文章，发表评论
5.结束

等待元素
d(text="Settings").wait(timeout=10.0)

长点击
d(text="Settings").long_click()

d.healthcheck()#这个很重要，每次执行脚本的时候运行这一句会检测设备端的准备工作是否到位。进程守护。

if d.info.get('screenOn')==False:#判断当前屏幕是否打开
    d.screen_on()
    sleep(2)#适当的加上延时，可以确保脚本的健壮，不容易崩溃
    d.unlock()#这个解锁好像不是每个手机上都能用

'''

import uiautomator2 as u2
import time
import random
import os
#d = u2.connect_usb("69DDU16601010543")  # 手机


def start_adb():
    os.system("adb devices")

time.sleep(3)

start_adb()
d = u2.connect("emulator-5554")  # 模拟器

d.unlock()
d.press('home')
d(text="Keep").click()

#首页
homepage = d.xpath('//*[@resource-id="com.gotokeep.keep:id/tabs"]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]')

homepage.click()

#热门
hot = d(text="热门")
hot.click()

contents = ["太帅了，还是太美了", "好帅气！", "好酷啊！", "棒", "健身加油，不错哦！"]
def say_something():
    # 说点什么
    time.sleep(2)
    d(resourceId="com.gotokeep.keep:id/textCommentInput").click()
    # d(resourceId="com.gotokeep.keep:id/textCommentInput").click()
    #d(resourceId="com.gotokeep.keep:id/text_hint").click()
    # d(resourceId="com.gotokeep.keep:id/textCommentInput").set_text("你的文章写的真好,图片配的也不错，健身加油，不错哦！")
    # 切换输入法atx,输入
    d.send_keys(contents[random.randint(0, 4)], clear=True)
    # 输入完关掉输入法
    #d.set_fastinput_ime(False)
    # click post_btn
    time.sleep(random.randint(2, 5))
    d(resourceId="com.gotokeep.keep:id/btn_send").click()


def go_back():
    #返回
    time.sleep(2)
    d(resourceId="com.gotokeep.keep:id/imgBack").click()

hot_content = d.xpath('//*[@resource-id="com.gotokeep.keep:id/refreshLayout"]/androidx.recyclerview.widget.RecyclerView[1]/android.widget.LinearLayout[1]')

def swipe(d):
    # d.touch.down(36, 919) # 模拟按下
    # time.sleep(.02) # down 和 move 之间的延迟，自己控制
    # d.touch.move(36, 639) # 模拟移动
    # d.touch.up(36, 639) # 模拟抬起

    #d.swipe(36, 919, 36, 639) # 一般九宫格解锁用
    time.sleep(random.randint(3, 7))
    d.swipe_ext("up", scale=0.2) # 屏幕宽度30%


swipe(d)
#c = d.xpath('//*[@resource-id="com.gotokeep.keep:id/recyclerView"]/android.view.ViewGroup[2]/android.view.View[1]')
#c = d(resourceId="com.gotokeep.keep:id/imgGif")
c = d.xpath('//*[@resource-id="com.gotokeep.keep:id/recyclerView"]/android.view.ViewGroup[1]/android.view.View[1]')
c.click()

for i in range(21):
    say_something()
    print("评论第%s" % i)
    swipe(d)
"""
try:
    for i in range(1, 7, 2):

        swipe(d)
        c = d.xpath('//*[@resource-id="com.gotokeep.keep:id/recyclerView"]/android.view.ViewGroup[%s]/android.view.View[1]'% i)
        # android.view.ViewGroup[%s]/android.view.View[1]'% i)
        if c:
            c.click()
            say_something()
            go_back()
            print(i)
            if i == 7:
                i = 2
                continue


finally:
    print("Stop!!!")
"""


#文章
time.sleep(1)
content = d.xpath('//*[@resource-id="com.gotokeep.keep:id/recyclerView"]/androidx.recyclerview.widget.RecyclerView[1]/android.view.View[1]')
#content.click()
d.xpath('//*[@resource-id="com.gotokeep.keep:id/recyclerView"]/androidx.recyclerview.widget.RecyclerView[1]/android.view.View[1]')




